package com.zz.schoolwall.controller;

import com.zz.common.utils.PageUtils;
import com.zz.common.utils.Query;
import com.zz.common.utils.R;
import com.zz.schoolwall.domain.SchoolWallCommentDO;
import com.zz.schoolwall.service.SchoolWallCommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/schoolwall/comment")
@Controller
public class SchoolWallCommentController {
	@Autowired
	SchoolWallCommentService schoolWallCommentService;



	@GetMapping("/edit/{id}")
	@RequiresPermissions("schoolwall:wall:edit")
	String editComment(@PathVariable("id") Long id, Model model) {
		model.addAttribute("id", id);
		return "schoolwall/info/edit_comment";
	}
	/**
	 * 墙话的评论查询
	 * @param wallId
	 * @return
	 */
	@ResponseBody
	@GetMapping("/listMain/{wallId}")
	public PageUtils listMain(@PathVariable("wallId") Long wallId,@RequestParam Map<String, Object> params) {
		params.put("wallId",wallId);
		params.put("commentStatus",1);
		params.put("parentCommentId",-1);
		Query query = new Query(params);
		List<SchoolWallCommentDO> reList = schoolWallCommentService.list(query);
		int total = schoolWallCommentService.count(query);
		PageUtils pageUtils = new PageUtils(reList, total);
		return pageUtils;
	}

	/**
	 * 评论的回复 查询
	 * @param parentCommentId
	 * @return
	 */
	@ResponseBody
	@GetMapping("/listSecond/{parentCommentId}")
	public PageUtils listSecond(@PathVariable("parentCommentId") Long parentCommentId,@RequestParam Map<String, Object> params ) {
		SchoolWallCommentDO comment=schoolWallCommentService.get(parentCommentId);
		if(comment.getCommentStatus()==-1){
			return new PageUtils(new ArrayList<>(), 0);
		}



		params.put("commentStatus",1);
		params.put("parentCommentId",parentCommentId);
		Query query = new Query(params);
		List<SchoolWallCommentDO> reList = schoolWallCommentService.list(query);
		int total = schoolWallCommentService.count(query);
		PageUtils pageUtils = new PageUtils(reList, total);
		return pageUtils;
	}
	/**
	 * 删除
	 */
	@RequiresPermissions("schoolwall:comment:remove")
	@PostMapping("/remove")
	@ResponseBody
	public R remove(Long id) {
		if (schoolWallCommentService.logicRemove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

}
